package it.slkfarm.android;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

import org.apache.http.HttpConnection;
import org.apache.http.HttpConnectionMetrics;
import org.apache.http.client.ClientProtocolException;

import com.sun.mail.util.BASE64DecoderStream;

import it.slkfarm.bean.Crop;
import it.slkfarm.bean.Farmer;
import it.slkfarm.bean.ProductionBean;
import it.slkfarm.helpers.DBAdapter;
import it.slkfarm.http.HttpConnector;
import it.slkfarm.log.AppLogger;
import it.slkfarm.log.SendLog;
import it.slkfarm.mail.MailSender;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;

public class Base extends Activity 
{


	public static final String IMEI_EMULATOR = "000000000000000";
	public static final String IMEI_TEST = "359043047263857";
	public static final String IMAGES_PATH = "/data/data/it.slkfarm.android/files/";
	public static final String JSON_REGISTRATION = "Registration";
	public static final String JSON_LOGIN = "Login";
	public static final String JSON_LOGIN_ACTION = "Login";
	public static final String JSON_REGISTRATION_ACTION = "Regst";
	public static final String WEB_SERVICE_URL_STAGES = "http://webe1.scem.uws.edu.au/index.php/agriculture/web_services/index/stages";
	public static final String WEB_SERVICE_URL_LOGIN= "http://webe1.scem.uws.edu.au/index.php/agriculture/web_services/index/registration";
	public static final String WEB_SERVICE_URL_CROP = "http://webe1.scem.uws.edu.au/index.php/agriculture/web_services/index/crop";
	public static final String WEB_SERVICE_URL_REGISTRATION= "http://webe1.scem.uws.edu.au/index.php/agriculture/web_services/index/registration";
	public static final String WEB_SERVICE_URL_PRODUCTION = "http://webe1.scem.uws.edu.au/index.php/agriculture/web_services/index/crop";
	public static final String TAG_STAGES = "Stages";
	public static final String TAG_ID = "id";
	public static final String TAG_NAME = "name";
	public static final String IMAGE_URL = "image_url";
	public static final String TAG_LOGIN = "login";
	public static final String TAG_REGISTER = "registerphonenumber";
	public static final String TAG_GET_CROPS = "getcrops";
	public static final String TAG_GET_CROPS_TEST = "getcropstest";
	public static final String TAG_PRODUCTION = "production";
	public static final String INSERT = "insert";
	public static final String UPDATE = "update";
	public static final String TAG_ADD_FARM = "addfarm";
	public static final int NO_LANG = 0;
	public static final int EN_LANG = 1;
	public static final int SRI_LANG = 2;
	public static final String LOCALE_SRILANKA = "si";
	public static final String LOCALE_EN = "en";
	/**
	 * @uml.property  name="dbAdapter"
	 * @uml.associationEnd  
	 */
	protected DBAdapter dbAdapter;
	/**
	 * @uml.property  name="logger"
	 * @uml.associationEnd  
	 */
	protected AppLogger logger;
	/**
	 * @uml.property  name="deviceId"
	 */
	protected String deviceId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		dbAdapter = new DBAdapter(this);
		dbAdapter.open();
		final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(this.TELEPHONY_SERVICE);
		deviceId = tm.getDeviceId();
		Log.i("device", deviceId);
		logger = new AppLogger(deviceId);
			
		File dir = new File(IMAGES_PATH);
		if (!dir.exists())
			new File(IMAGES_PATH).mkdir();
	}
	
	
	
	@Override
	protected void onDestroy() 
	{
		super.onDestroy();
		dbAdapter.close();
	}
	
	public void stopService()
	{
		Intent intent = new Intent(this, SendLog.class);
		stopService(intent);
	}
	
	public void startService()
	{
		Intent intent = new Intent(this, SendLog.class);
		intent.putExtra(getPackageName()+".id", deviceId);
		startService(intent);
	}
	
	
	public void setLanguage(int lang)
	{
		Resources res = this.getResources();
	    DisplayMetrics dm = res.getDisplayMetrics();
	    android.content.res.Configuration conf = res.getConfiguration();
		if (lang == EN_LANG)
			conf.locale = Locale.getDefault();
		else 
			conf.locale = new Locale(LOCALE_SRILANKA);
		res.updateConfiguration(conf, dm);
	}
	
	public Typeface getTypeface()
	{
		if (dbAdapter.getSetLanguage() == EN_LANG)
			return Typeface.createFromAsset(getAssets(),"fonts/KMKDSP__.ttf");
		else
			return null;
	}
}